﻿using System;
using System.Collections.Generic;
using KinectFundamentals.GestureRecognizer;
using Microsoft.Kinect;

namespace KinectFundamentals.Gestures {
    public class SwipeToFromScreen  : AbstractGestureRecognizer {
        public float SwipeMinimalLength { get; set; }
        public float SwipeMaximalHeight { get; set; }
        public float SwipeMaximalWidth { get; set; }
        public int SwipeMininalDuration { get; set; }
        public int SwipeMaximalDuration { get; set; }

        private int _start = 0;
        private readonly DirectionEnum _direction;

        public SwipeToFromScreen(JointEnum source, DirectionEnum direction, DataStreamsDictionary dataStreamsDictionary) {
            base.GestureRecognizer(source, dataStreamsDictionary);
            _direction = direction;
            SwipeMinimalLength = 0.3f;
            SwipeMaximalWidth = 0.4f;
            SwipeMaximalHeight = 0.4f;
            SwipeMininalDuration = 250;
            SwipeMaximalDuration = 1500;
        }


        public override void DetectGesture() {
            var entries = DataStreamsDictionary.GetEntriesList(JointEnum);
            for (int i = _start + 1; i < entries.Count - 1; i++) {
                float x = entries[i].Position.X;
                float y = entries[i].Position.Y;
                float z = entries[i].Position.Z;
                float xb = entries[i - 1].Position.X;
                float yb = entries[i - 1].Position.Y;
                float zb = entries[i - 1].Position.Z;
                float xs = entries[_start].Position.X;
                float ys = entries[_start].Position.Y;
                float zs = entries[_start].Position.Z;

                // check swipe direction
                if (_direction == DirectionEnum.ToScreen) {
                    if (z > zb) {
                        _start = i;
                        break;
                    }
                }
                    // todo not implemented
                else {
                    if (x < xb) {
                        _start = i;
                        break;
                    }
                }

                // check movement y-Direction
                if (Math.Abs(y - ys) > SwipeMaximalHeight) {
                    _start = i;
                    break;
                }

                // check movement y-Direction
                if (Math.Abs(x - xs) > SwipeMaximalWidth) {
                    _start = i;
                    break;
                }

                // check swipe length
                if (Math.Abs(z - zs) <= SwipeMinimalLength) continue;

                // check duration
                var duration = (entries[i].Time - entries[_start].Time).TotalMilliseconds;

                if (duration <= SwipeMininalDuration || duration >= SwipeMaximalDuration) {
                    _start = i;
                }
                else {
                    //Console.WriteLine("Swipe -- " + _direction.ToString() + "-- " + _jointEnum);
                    RaiseGestureDetected("Swipe", JointEnum, _direction, entries[_start].Time, entries[i].Time);
                    _start = i;
                }
            }
        }
    }
}